package com.adtech.adms.system.web.action;

import java.util.List;

import com.adtech.adms.commons.ErrorCode;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.system.pojo.Feature;
import com.adtech.adms.system.pojo.Role;
import com.adtech.adms.system.service.FeatureService;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.web.action.AjaxResult;
import com.adtech.core.web.action.BaseAction;

/**
 * 菜单action处理类
 * @author 门宏建 
 * @since 2011-6-13 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class FeatureAction extends BaseAction {

	private static final long serialVersionUID = 6045306463410230082L;

	// 管理器
	private FeatureService featureService;
	private Feature feature;

	private Long id;
	private List<TreeNode<Feature>> nodes;
	private boolean allLevel = true;
	private Role role;

	/**
	 * @Function 列表初始页面的转发
	 * @param
	 * @return String
	 * @exception
	 * 
	 * @author dormin
	 * @changed
	 * 
	 */
	@Override
	public String execute() {
		return SUCCESS;
	}

	/**
	 * 保存
	 * 
	 * @return String
	 * @throws ServiceException
	 * 
	 * @author dormin
	 */
	public String save() throws ServiceException {
		featureService.save(feature);
		setAjaxResult(AjaxResult.success());
		return SUCCESS;
	}

	/**
	 * @Function 获取具体信息
	 * @param
	 * @return String
	 * @exception
	 * 
	 * @author dormin
	 * @changed
	 * 
	 */
	public String getFeatureDetail() {
		if (id != null) {
			feature = featureService.getFeatureById(id);
		}
		return SUCCESS;
	}

	/**
	 * @Function 删除
	 * @param
	 * @return String
	 * @throws ServiceException
	 * @exception
	 * 
	 * @author dormin
	 * @changed
	 * 
	 */
	public String delete() throws ServiceException {
		if (id != null) {
			featureService.delete(id);
		}
		setAjaxResult(AjaxResult.success());
		return SUCCESS;
	}

	/**
	 * @Function 获取树结构
	 * @param
	 * @return String
	 * @exception
	 * 
	 * @author dormin
	 * @changed
	 * 
	 */
	public String getFeatureTree() {
		if (id == null) {
			nodes = featureService.getFeatureTree(allLevel,role);
		} else {
			nodes = featureService.getFeatureTree(id, allLevel,role);
		}
		return SUCCESS;
	}

	public String getCheck() {
		if (id != null) {
			feature.setId(id);
		}
		
		if (featureService.check(feature)) {
			setAjaxResult(AjaxResult.success());
		} else {
			setAjaxResult(AjaxResult.failed(ErrorCode.FEATURE_DUPLICATE));
		}
		return SUCCESS;
	}

	public void setFeatureService(FeatureService fetureService) {
		this.featureService = fetureService;
	}

	public Feature getFeature() {
		return feature;
	}

	public void setFeature(Feature feature) {
		this.feature = feature;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<TreeNode<Feature>> getNodes() {
		return nodes;
	}

	public void setNodes(List<TreeNode<Feature>> nodes) {
		this.nodes = nodes;
	}

	public boolean isAllLevel() {
		return allLevel;
	}

	public void setAllLevel(boolean allLevel) {
		this.allLevel = allLevel;
	}
	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

}
